package com.weilele.leisure.time.ui.dialog


import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.FrameLayout
import com.weilele.base.library.BaseBindingBottomSheetDialog
import com.weilele.leisure.time.R
import com.weilele.leisure.time.bean.VideoDetailBean
import com.weilele.leisure.time.databinding.DialogCacheListBinding
import com.weilele.leisure.time.ui.holder.CacheListItemHolder
import com.weilele.leisure.time.ui.module.video_detail.VideoDetailPlayerHelper
import com.weilele.leisure.time.ui.module.video_detail.VideoInfoFragment
import com.weilele.mvvm.adapter.refreshList
import com.weilele.mvvm.utils.activity.findFragment
import com.weilele.mvvm.utils.activity.getActivity
import com.weilele.mvvm.utils.activity.getBindFragment
import com.weilele.mvvm.utils.safeGet
import com.weilele.mvvm.utils.toJsonString
import com.yizisu.playerlibrary.service.download.PlayerDownload


class CacheListDialog : BaseBindingBottomSheetDialog<DialogCacheListBinding>() {
    companion object {
        fun show(
            view: View,
            list: MutableList<VideoDetailBean.PlayersDTO.DatasDTO>?,
            info: VideoDetailBean.PlayersDTO?,
            videoInfo: VideoDetailBean?
        ): CacheListDialog? {
            if (list.isNullOrEmpty()) {
                return null
            }
            return CacheListDialog().apply {
                currentPlayList = list
                videoInfoBean = videoInfo
                playersDTO = info
                show(view.getActivity())
            }
        }
    }

    private var currentPlayList: MutableList<VideoDetailBean.PlayersDTO.DatasDTO>? = null
    private var videoInfoBean: VideoDetailBean? = null
    private var playersDTO: VideoDetailBean.PlayersDTO? = null

    private val listAdapter = CacheListItemHolder.toAdapter().apply {
        setOnHolderClickListener { holder, clickView ->
            holder.dataWhenBind?.let { data ->
                val contentId = data.video.playUrl
                PlayerDownload.startOrResumeDownload(
                    contentId, Uri.parse(contentId),
                    buildBlock = {
                        it.setData(
                            VideoDetailPlayerHelper.toHistoryVideoBean(
                                videoInfoBean, playersDTO, data.video, 0, 0
                            ).toJsonString()
                                .toByteArray()
                        )
                    }
                )
                holder.safeGet<CacheListItemHolder>()
                    ?.showDownloadInfo(PlayerDownload.getDownload(contentId))
            }
        }
    }

    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        val datas = mutableListOf<CacheListItemHolder.Bean>()
        currentPlayList?.forEach { video ->
            datas.add(
                CacheListItemHolder.Bean(
                    video,
                    PlayerDownload.getDownload(video.playUrl)
                )
            )
        }
        mBinding.tvName.text = "${getString(R.string.cache_line)}${playersDTO?.name}"
        mBinding.cacheRcv.adapter = listAdapter
        listAdapter.refreshList(datas)
        mBinding.fgVideoInfo.post {
            videoInfoBean?.let {
                mBinding.fgVideoInfo.getBindFragment<VideoInfoFragment>(this)?.updateInfo(it)
            }
        }
    }

    override fun onRootViewLayoutParams(lp: FrameLayout.LayoutParams) {
        super.onRootViewLayoutParams(lp)
        lp.marginStart = 0
        lp.marginEnd = 0
    }
}